package com.maxd.service.selfAdvert;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.Adverti;
import com.maxd.respository.selfAdvert.AdvertJpaRepository;
import com.maxd.respository.selfAdvert.AdvertRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AdvertServiceImpl implements AdvertService {
    @Autowired
    private AdvertJpaRepository jpaRepository;
    @Autowired
    private AdvertRepository advertRepository;

    @Override
    public Result findAll(Integer type) {
        if(type==null){
            return ResultUtil.success(advertRepository.selectSelfBanner());
        }else{
            return ResultUtil.success(advertRepository.selectSelfBanner(type));
        }
    }

    @Override
    public Result saveBody(Adverti entity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        entity.setCreateAt(sdf.format(now));
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result updateBody(Adverti entity) {
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result findOne(Long id) {
        return ResultUtil.success(jpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(Long id) {
        jpaRepository.deleteById(id);
        return ResultUtil.success();
    }

}
